import React, { Component } from 'react';
import { Popover, Button, Select, message } from 'antd';
import moment from 'moment';
import styles from './index.less';
import * as apis from '@/services/SystemMgr';

const { Option } = Select;
export default class MyPopover extends Component {
  constructor(props) {
    super(props);
    this.state = {
      visible: false,
      time: '',
      options: [],
    };
  }

  componentDidMount() {
    // 获取当月天数
    const options = [];
    const getMonthDay = moment().daysInMonth();
    for (let i = 1; i <= getMonthDay - 3; i++) {
      options.push(`每月${i}号`);
    }
    options.push('月底前两天');
    options.push('月底前一天');
    options.push('月底当天');
    this.setState({
      options,
      time: options[0],
    });
  }

  hide = () => {
    this.setState({
      visible: false,
    });
  };

  handleVisibleChange = visible => {
    this.setState({ visible });
  };

  // 获取推送时间值
  handleGetDateChange = value => {
    this.setState({ time: value });
  };

  // 点击确定
  handleOkClick = async recordID => {
    const { time } = this.state;
    const params = {
      pushTime: time,
      id: recordID,
    };
    const data = await apis.updateSysDeliveryPlanUsingPUT({ data: params });
    if (data.code && data.code === 1) {
      message.success('推送成功！');
      this.props.upDate();
    } else {
      message.error(data.message);
    }
    this.setState({
      visible: false,
    });
  };

  render() {
    const { systemData, recordID } = this.props;
    const { options } = this.state;
    return (
      <Popover
        placement="left"
        content={
          <div className={styles.popoverContent}>
            <div>
              <span>推送时间:</span> <br />
              <Select
                style={{ width: 160 }}
                defaultValue={options[0]}
                onSelect={value => this.handleGetDateChange(value)}
              >
                {options.length > 0
                  ? options.map(value => <Option key={value}>{value}</Option>)
                  : null}
              </Select>
            </div>
            <div>
              <Button type="primary" onClick={() => this.handleOkClick(recordID)}>
                确定
              </Button>
              <Button onClick={this.hide}>取消</Button>
            </div>
          </div>
        }
        trigger="click"
        visible={this.state.visible}
        onVisibleChange={this.handleVisibleChange}
      >
        <Button type="link">设置推送时间</Button>
      </Popover>
    );
  }
}
